package org.cloudie.docs.editor.client.content.contextmenus;

import org.cloudie.docs.editor.client.commands.generic.VirtualEntryRecycleCommand;
import org.cloudie.docs.editor.client.commands.generic.VirtualEntryRemoveCommand;
import org.cloudie.docs.editor.client.commands.generic.VirtualEntryRenameCommand;
import org.cloudie.docs.editor.client.commands.generic.WindowRestoreCommand;
import org.cloudie.docs.editor.client.events.CommandEvent;
import org.cloudie.docs.editor.client.resources.icons.IconsHelper;
import org.cloudie.docs.editor.client.widgets.ContextMenu;
import org.cloudie.docs.editor.client.widgets.DesktopIcon;

import com.google.gwt.user.client.Command;

public class IconContextMenu extends ContextMenu {

  protected static IconContextMenu instance;
  
  public static IconContextMenu get() {
    if (instance == null) {
      instance = new IconContextMenu();
    }
    return instance;
  }
	  
  protected DesktopIcon target;
	
  protected IconContextMenu() {
    addMenuItem(IconsHelper.icons.Blank(), "Open", new Command() {
      public void execute() {
        if (target != null) {
    	  CommandEvent.fire(target.getCommand());
    	  if (IconContextMenu.this.target.getCommand().getCommandId() ==
              WindowRestoreCommand.serialUid) {
    	    CommandEvent.fire(
    	        new VirtualEntryRemoveCommand(IconContextMenu.this.target.getId()));
          }
    	}
      }
    });
    addMenuItem(IconsHelper.icons.Blank(), "Rename", new Command() {
	  @Override
	  public void execute() {
    	if (target != null) {
      	  CommandEvent.fire(
      	      new VirtualEntryRenameCommand(IconContextMenu.this.target.getId(),
      	        null, IconContextMenu.this.target.getLabel()));
    	}
	  }
    });
	addMenuItem(IconsHelper.icons.Blank(), "Delete", new Command() {
      @Override
	  public void execute() {
    	if (target != null) {
    	  CommandEvent.fire(new VirtualEntryRecycleCommand(IconContextMenu.this.target.getId()));
    	}
	  }
	});
  }

  public DesktopIcon getTarget() {
	return target;
  }

  public void setTarget(DesktopIcon target) {
	this.target = target;
  }

}
